﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Credisoft.Models.Helpers;
using Credisoft.Models.Credisoft_DAO;
using Credisoft.Models;
using Credisoft.Models.Credisoft_BO;
namespace Credisoft.Controllers.Inbox
{
    public class InboxController : Controller
    {
        //
        // GET: /Inbox/

        public ActionResult Inbox()
        {
            return View();
        }


        public ActionResult ApprovalFormModal()
        {
            object model = null;
            var obj = new { view = RequestHelper.PartialView(this, "ApprovalFormModal", model)};
            return Json(obj);
        }

       //Aqui se recibe si la solicitud ha sido aprobada o rechazada
        public Boolean ApprovalFormAnswer(int idAnswer,int idRequest, int idUser)
        {
        

            return true;
        }




        public ActionResult CreditRequestAsignationModal(int idRequest, int idGroupChief)
        {
            //object model = new ModeloPruebaTopo(); // whatever you want baybeeee
            WorkgroupDAO workgroupDAO = new WorkgroupDAO();
            UserDAO userDAO = new UserDAO();
            grupotrabajo workgroup = workgroupDAO.getWordgroupByChief(idGroupChief);
            this.ViewBag.workgroup = workgroup;
            if (workgroup == null)
            {
                return Json("Error al obtener el grupo de trabajo.");
            }
            List<usuario> memberList = workgroupDAO.getMembers(workgroup.idgrupotrabajo);
            //Delete groupchief
            List<double> workloads = new double[5].ToList();
            int counter = 1;
            foreach (var user in memberList)
            {
                workloads.Insert(counter,userDAO.getWorkLoad(user)); 
                counter++;
            }
            this.ViewBag.memberList = memberList;
            this.ViewBag.workloads = workloads;
            object model = null;
            var obj = new { view = RequestHelper.PartialView(this, "CreditRequestAsignationModal", model) , title = "Asignación de Solicitud de Crédito"};
            return Json(obj);
        }
        public ActionResult UpdateMailbox4()
        {
            List<votorgamientosusuario> response = null;// CreditGrantingDAO.getGratingByUser();
            var obj = new { response = response };
            return Json(obj);
        }

        public ActionResult UpdateMailbox2(int userId)
        {
            votorgamientosusuario userGrantingView = new votorgamientosusuario();
            userGrantingViewBO userGrantingViewBO = new userGrantingViewBO();
            userGrantingViewDAO userGrantingViewDAO = new userGrantingViewDAO();
            List<votorgamientosusuario> userGrantingCreditList = userGrantingViewDAO.getUserGrantingCreditData(userId);

            //CreditGrantingDAO creditGrantingDAO = new CreditGrantingDAO();
            //List<otorgamiento> grantingList = creditGrantingDAO.getCreditGrantingData();
            EnterpriseDAO enterpriseDAO = new EnterpriseDAO();
            //List<EnterpriseBO> enterpriseBOList = new List<EnterpriseBO>();
            //EconomicGroupDAO economicGroupDAO = new EconomicGroupDAO();
            //List<EconomicGroupBO> economicGroupBOList = new List<EconomicGroupBO>();
            var objList = new List<object>();

            //int j = 0;
            //foreach (var i in grantingList)
            //{
            //    enterpriseBOList.Add(enterpriseDAO.getEnterpriseData((int)i.identidad));
            //    int groupId = enterpriseBOList.ElementAt(j).groupId;
            //    economicGroupBOList.Add(economicGroupDAO.getEconomicGroupData(enterpriseBOList.ElementAt(j).groupId));
            //    var obj = new
            //    {
            //        requestId = grantingList.ElementAt(j).idotorgamiento,
            //        socialReason = enterpriseBOList.ElementAt(j).socialReason,
            //        group = economicGroupBOList.ElementAt(j).name,
            //        recordDate = (grantingList.ElementAt(j).fecharegistro).ToString("dd/MM/yyyy")
            //    };
            //    j++;
            //    objList.Insert(0, obj);
            //}

            foreach (var pivot in userGrantingCreditList)
            {
                var obj = new
                {
                    requestId = pivot.idotorgamiento,
                    socialReason = pivot.empresa,
                    group = enterpriseDAO.getEnterpriseData(pivot.empresa).idgrupo,
                    recordDate = pivot.fecharegistro
                };
                objList.Insert(0, obj);
            };
            return Json(objList);
        }
    }
}
